const dateTimePicker = require("./dateTimePicker.js");
const { FormatDate } = require("../../../utils/FormDate");
Component({
  properties: {
    placeholder: {
      type: String,
      value: "请输入",
    },
    title: String,
    required: Boolean,
    disabled: Boolean,
    readonly: Boolean,
    showLink: Boolean,
    inset: Boolean,
    marginBottom: {
      type: String,
      value: "20rpx",
    },
    value: {
      type: null,
      value: "",
    },
    timeType: String,
    showClearBtn: {
      type: Boolean,
      value: true,
    },
  },
  data: {
    dateTimeArray: [],
  },
  lifetimes: {
    attached() {
      const array = dateTimePicker.dateTimePicker(this.data.timeType);
      this.setData({
        dateTimeArray: array.dateTimeArray,
      });
    },
  },

  methods: {
    changeDateTime(e) {
      this.setValue(e.detail.value);
    },
    setValue(value) {
      const date = this.data.dateTimeArray[0][value[0]];
      let result = "";
      if (this.data.timeType === "slot") {
        const slotData = this.data.dateTimeArray[1][value[1]];
        result = `${date} ${slotData}`;
      } else {
        const hour = this.data.dateTimeArray[1][value[1]];
        const minute = this.data.dateTimeArray[2][value[2]];
        result = `${date} ${hour}:${minute}`;
      }

      this.setData({
        value: result,
      });

      this.triggerEvent("change", result);
    },
    bindMultiPickerColumnChange(e) {
      const column = e.detail.column;
      const currentIndex = e.detail.value;
      if (column !== 0) return;
      const array = dateTimePicker.dateTimePicker(this.data.timeType, currentIndex);
      const data = {
        dateTimeArray: array.dateTimeArray,
      };
      this.setData(data);
    },
    onClick() {
      let value = new FormatDate().format("yyyy-MM-dd hh:mm");

      if (this.data.value) {
        value = this.data.value;
      }
      const currentValue = value.replace(":", " ").split(" ");
      const valueArray = this.data.dateTimeArray.map((item, index) => {
        return item.findIndex((f) => f + "" === currentValue[index] + "");
      });

      this.setData({
        currentValue: valueArray,
      });
    },

    onClear() {
      this.triggerEvent("change", "");
    },
  },
});
